package com.wing.product.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.annotation.RequestObjectFieldIgnore;
import com.wing.common.constant.SecurityConstants;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.WarehouseProductionSchedulingPlan;
import com.wing.product.model.response.WarehouseProductionSchedulingPlanVO;
import com.wing.product.service.WarehouseProductionSchedulingPlanService;
import com.wing.web.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Api(value = "生产排产计划管理", tags = "#/warehouse/product/warehouseProductionSchedulingPlan")
@RestController("WarehouseProductionSchedulingPlanController")
@RequestMapping("/warehouse/productionSchedulingPlan")
@RequiredArgsConstructor
public class WarehouseProductionSchedulingPlanController {

    private final WarehouseProductionSchedulingPlanService warehouseProductionSchedulingPlanService;

    @ApiOperation(value = "查询所有", notes = "搜索")
    @GetMapping("/list")
    public PageResult<WarehouseProductionSchedulingPlanVO> list(
            @RequestParam(value = "page", defaultValue = "1", required = false) Integer page
            ,@RequestParam(value = "limit", defaultValue = "10", required = false) Integer limit
            ,@RequestParam(value = "sort", defaultValue = "create_time", required = false) String sort
            ,@RequestParam(value = "order", defaultValue = "asc", required = false) String order,
            String productionPlanCode,
            String customerName,
            String productName,
            String status,
            Long customerId
            ,String startCreateTime
            ,String endCreateTime
            ,String userBundleCode
    ){
        if(JwtUtils.getClientId().equals(SecurityConstants.WAREHOUSE_CLIENT_ID)){
            customerId = JwtUtils.getDepartmentId();
        }
        Page<WarehouseProductionSchedulingPlan> pg = new Page<>(page, limit);
        Map paramMap=new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", order);
        paramMap.put("productionPlanCode", productionPlanCode);
        paramMap.put("userBundleCode", userBundleCode);
        paramMap.put("productName", productName);
        paramMap.put("customerName", customerName);
        paramMap.put("status", status);
        paramMap.put("customerId", customerId);
        if (com.alibaba.nacos.common.utils.StringUtils.isNotBlank(startCreateTime)) {
            paramMap.put("startCreateTime", startCreateTime);
        }
        if (com.alibaba.nacos.common.utils.StringUtils.isNotBlank(endCreateTime)) {
            paramMap.put("endCreateTime", endCreateTime);
        }
        //搜索
        IPage<WarehouseProductionSchedulingPlanVO> list = warehouseProductionSchedulingPlanService.pageList4app(pg,paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }

    @ApiOperation(value = "查询生产计划明细", notes = "查询")
    @GetMapping ("/selectProductionSchedulingPlanDetail")
    public JsonResult<WarehouseProductionSchedulingPlanVO> selectProductionSchedulingPlanDetail(@RequestParam String id){
        if (!StringUtils.isEmpty(id)){
            WarehouseProductionSchedulingPlanVO warehouseProductionSchedulingPlanVO = warehouseProductionSchedulingPlanService.selectProductionSchedulingPlanDetail(id);
            if (ObjectUtil.isNotEmpty(warehouseProductionSchedulingPlanVO)){
                return  JsonResult.success("请求成功",warehouseProductionSchedulingPlanVO);
            }else {
                return  JsonResult.success("数据为空",warehouseProductionSchedulingPlanVO);
            }
        }else {
            return  JsonResult.fail("id为空");
        }
    }

    @ApiOperation(value = "修改生产计划", notes = "修改")
    @RequestObjectFieldIgnore({"id", "remark"})
    @PostMapping("/update")
    public JsonResult update(@RequestBody WarehouseProductionSchedulingPlanVO warehouseProductionSchedulingPlanVO)  {
        try{
            return warehouseProductionSchedulingPlanService.updateWarehouseProductionSchedulingPlan(warehouseProductionSchedulingPlanVO);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "添加生产计划", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@RequestBody WarehouseProductionSchedulingPlanVO warehouseProductionSchedulingPlanVO)  {
        try{
            return warehouseProductionSchedulingPlanService.saveWarehouseProductionSchedulingPlan(warehouseProductionSchedulingPlanVO);
        }catch (Exception e){
            return JsonResult.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "修改状态", notes = "修改")
    @GetMapping ("/updateProductionSchedulingPlan")
    public JsonResult updateProductionSchedulingPlan(@RequestParam String id,@RequestParam String status){
        if (!StringUtils.isEmpty(id)){
            return warehouseProductionSchedulingPlanService.updateProductionSchedulingPlan(id,status);
        }else {
            return  JsonResult.fail("id为空");
        }
    }
}
